{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Your first Classiq model\n",
    "\n",
    "Here we will create your first Classiq model. We will create a Quantum program that will perfrom an addition between an integer and superpostion of integers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:10.136054Z",
     "iopub.status.busy": "2024-05-07T14:27:10.135601Z",
     "iopub.status.idle": "2024-05-07T14:27:10.142105Z",
     "shell.execute_reply": "2024-05-07T14:27:10.141445Z"
    }
   },
   "outputs": [],
   "source": [
    "# !pip install -U classiq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:10.145425Z",
     "iopub.status.busy": "2024-05-07T14:27:10.144954Z",
     "iopub.status.idle": "2024-05-07T14:27:12.940091Z",
     "shell.execute_reply": "2024-05-07T14:27:12.939284Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import *\n",
    "\n",
    "# If you have not yet authenticated please uncomment the line below\n",
    "# authenticate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:12.945140Z",
     "iopub.status.busy": "2024-05-07T14:27:12.943852Z",
     "iopub.status.idle": "2024-05-07T14:27:15.510470Z",
     "shell.execute_reply": "2024-05-07T14:27:15.509806Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/b39feba2-d22e-4d2e-a5a7-03c097d59ed5?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "# QFunc declares a quantum function\n",
    "@qfunc\n",
    "# The main quantum function that will produce 3 outputs.\n",
    "def main(res: Output[QNum], a: Output[QNum], b: Output[QNum]):\n",
    "    # Encode the integer value 3 in variable a\n",
    "    prepare_int(3, a)\n",
    "\n",
    "    # Encode the super position of 0 and 3 into variable b\n",
    "    prepare_state([0.5, 0, 0, 0.5], out=b, bound=0.01)\n",
    "\n",
    "    # Add a and b togehter, and save the result in res\n",
    "    res |= a + b\n",
    "\n",
    "\n",
    "# Create a quantum circuit from the model above\n",
    "qprog = synthesize(create_model(main))\n",
    "\n",
    "# Open the circuit in the IDE, where it can be analyzed and executed\n",
    "show(qprog)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Execution time\n",
    "\n",
    "Now that we have created a model, it is time to execute it. With the `show()` command the IDE is opened where we can execute the circuit. A page like this should have opened:\n",
    "\n",
    "<img src=\"\" />\n",
    "\n",
    "Click on the execute button on the right top. Next, you will get to the execution page where you can run the circuit, validate that you are running on the Classiq Aer simulation as highlighted in the image below, and press the Run button as seen below.\n",
    "\n",
    "<img src=\"\" />\n",
    "\n",
    "This should be the result of that circuit:\n",
    "\n",
    "<img src=\"\" />\n",
    "\n",
    "As you can see, there are two answers found to the equation res = a + b.\n",
    "\n",
    "1. 3 + 0 = 3\n",
    "2. 3 + 3 = 6\n",
    "\n",
    "So, our first quantum algorithm has done exactly what we wanted to do. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Do it yourself\n",
    "\n",
    "Initialize the input variables in these ways:\n",
    "\n",
    "- `a`: A superposition with an equal possibility to get a 0, 2 or 3.\n",
    "- `b`: 25% chance of 0 and 75% of 3\n",
    "- `c`: 50/50 chance of a 0 or 1\n",
    "\n",
    "The result variable should hold the result of `(a * b) + c`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:15.546596Z",
     "iopub.status.busy": "2024-05-07T14:27:15.546032Z",
     "iopub.status.idle": "2024-05-07T14:27:16.822040Z",
     "shell.execute_reply": "2024-05-07T14:27:16.821065Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/d5a3e1a4-0178-404e-8ac2-96c703c54111?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "@qfunc\n",
    "def main(\n",
    "    # Uncomment the variables below\n",
    "    # a: Output[QNum],\n",
    "    # b: Output[QNum],\n",
    "    # c: Output[QNum],\n",
    "    # res:Output[QNum]\n",
    "):\n",
    "    # Your code here\n",
    "    pass\n",
    "\n",
    "\n",
    "qprog = synthesize(create_model(main))\n",
    "show(qprog)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The full solution for your reference\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T14:27:16.826224Z",
     "iopub.status.busy": "2024-05-07T14:27:16.824904Z",
     "iopub.status.idle": "2024-05-07T14:27:19.102738Z",
     "shell.execute_reply": "2024-05-07T14:27:19.102076Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/cf2e3aa9-5411-4fea-9bb5-22860475f486?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "@qfunc\n",
    "def main(a: Output[QNum], b: Output[QNum], c: Output[QNum], res: Output[QNum]):\n",
    "    prepare_state([1 / 3, 0, 1 / 3, 1 / 3], out=a, bound=0.1)\n",
    "    prepare_state([1 / 4, 0, 0, 3 / 4], out=b, bound=0.1)\n",
    "    prepare_state([1 / 2, 1 / 2], out=c, bound=0.1)\n",
    "\n",
    "    res |= (a * b) + c\n",
    "\n",
    "\n",
    "qprog = synthesize(create_model(main))\n",
    "show(qprog)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
